package cn.wolfcode.shop.cloud.mq;


import cn.wolfcode.shop.cloud.service.ISeckillGoodService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@RocketMQMessageListener(
        consumerGroup = MQConstans.CREATE_FAIL_GROUP,
        topic = MQConstans.CREATE_ORDER,
        selectorExpression = MQConstans.CREATE_FAIL_TAG
)
@Component
public class OrderFailMQListener  implements RocketMQListener<Map> {

    @Autowired
    private ISeckillGoodService seckillGoodService;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Override
    public void onMessage(Map map) {
        //进入到失败的队列
        System.out.println("进入到失败队列中");

        Long goodId = null;
        Object seckillId =map.get("seckillId");
        if(seckillId instanceof Integer){
            goodId = ((Integer) seckillId).longValue() ;
        }else if (seckillId instanceof Long){
            goodId = (Long) seckillId;
        }

        //回补预库存
        seckillGoodService.syncRedisCount(goodId);

        //发送广播通知 修改本地标识
        rocketMQTemplate.syncSend(MQConstans.CREATE_NOTIFY_DEST,goodId);
    }
}
